package com.hd.appcommon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.constant.PrivilegeConstant;
import com.hd.appcommon.entity.Privilege;
import com.hd.appcommon.mapper.PrivilegeMapper;
import com.hd.appcommon.service.IPrivilegeService;
import com.hd.hdxgddcommon.constant.BaseConstant;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2020-11-26
 */
@Service
public class PrivilegeServiceImpl extends ServiceImpl<PrivilegeMapper, Privilege> implements IPrivilegeService {

    @Override
    public boolean save(Privilege privilege) {
        if (privilege != null) {
            String parentId = privilege.getParentId();
            List<Privilege> privileges = this.listByParentId(parentId);
            if (!privileges.isEmpty()) {
                String realId = privileges.get(0).getPrivilegeId();
                String realId0 = PrivilegeConstant.createRealId(realId);
                privilege.setPrivilegeId(realId0);
            } else {
                if (PrivilegeConstant.ROOT_PRIVILEGE_ID.equals(parentId))
                    privilege.setPrivilegeId(PrivilegeConstant.DEFAULT_PRIVILEGE_ID);
                else {
                    privilege.setPrivilegeId(parentId + PrivilegeConstant.CONN_CHAR + PrivilegeConstant.DEFAULT_PRIVILEGE_ID);
                }
            }
        }
        Date date = new Date();
        privilege.setCreateDate(date)
                .setModifyDate(date)
                .setDeletable(BaseConstant.DELETE_STATE.UN_DELETE);
        return super.save(privilege);
    }

    @Override
    public List<Privilege> listAllAble() {
        LambdaQueryWrapper<Privilege> lambda = new QueryWrapper<Privilege>().lambda();
        lambda.eq(Privilege::getDeletable, 1).orderByAsc(Privilege::getPrivilegeRank);
        return this.baseMapper.selectList(lambda);
    }

    @Override
    @Cacheable(value = "PrivilegeCache", key = "#p0")
    public List<Privilege> listByRoleId(String roleId) {
        return this.baseMapper.listByRoleId(roleId, BaseConstant.DELETE_STATE.UN_DELETE);
    }

    @Override
    public List<Privilege> listByParentId(String parentId) {
        LambdaQueryWrapper<Privilege> lambda = new LambdaQueryWrapper<>();
        lambda.eq(Privilege::getParentId, parentId).orderByDesc(Privilege::getPrivilegeId);
        return this.list(lambda);
    }

    @Override
    public boolean removeByIdCascade(String privilegeId) {
        LambdaQueryWrapper<Privilege> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(Privilege::getPrivilegeId, privilegeId);
        return this.remove(wrapper);
    }

    @Override
    public Privilege getUnique(Privilege privilege) {
        LambdaQueryWrapper<Privilege> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Privilege::getParentId, privilege.getParentId())
                .and(i -> i.eq(Privilege::getPrivilegeKey, privilege.getPrivilegeKey()).
                        or().eq(Privilege::getPrivilegeName, privilege.getPrivilegeName()));
        return this.getOne(wrapper);
    }

}
